package com.weimei.conversionTreasure.viewModel;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.umeng.commonsdk.debug.I;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.http.entity.BaseResult;
import com.weimei.conversionTreasure.http.entity.SelKhBean;
import com.weimei.conversionTreasure.utils.UserUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class SelKhVM extends TitleVM{
    public SelKhVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("客户记录");
    }

    public ObservableField<String> type = new ObservableField<>("");
    public ObservableInt jxsRecy = new ObservableInt(View.GONE);
    public ObservableInt khRecy = new ObservableInt(View.GONE);
    Activity mActivity;
    @Override
    public void setActivity(Activity activity) {
        super.setActivity(activity);
        this.mActivity = activity;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (type.get().equals("0")){
            jxsRecy.set(View.VISIBLE);
            khRecy.set(View.GONE);
            getJxsData();
        }else {
            jxsRecy.set(View.GONE);
            khRecy.set(View.VISIBLE);
            getKhData();
        }

    }

    public ObservableList<SelKhItemVM> observableListKh = new ObservableArrayList<>();
    public ItemBinding<SelKhItemVM> itemBindingKh = ItemBinding.of(BR.viewModel, R.layout.item_sel_kh);

    public ObservableList<SelKhItemVM> observableListJxs = new ObservableArrayList<>();
    public ItemBinding<SelKhItemVM> itemBindingJxs = ItemBinding.of(BR.viewModel, R.layout.item_sel_kh);

    private void getKhData() {
        observableListKh.clear();
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        model.custom_sort(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<SelKhBean>() {
                    @Override
                    public void onNext(SelKhBean selKhBean) {
                        if (selKhBean.issucc){
                            if (selKhBean.getData().get(0).getAppoints().size()>0){
                                for (SelKhBean.DataDTO.AppointsDTO data:selKhBean.getData().get(0).getAppoints()) {
                                    SelKhItemVM itemVM = new SelKhItemVM(SelKhVM.this,data);
                                    observableListKh.add(itemVM);
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getJxsData() {
        observableListJxs.clear();
        Map<String,String> map = new HashMap<>();
        map.put("user_id", UserUtils.getUserAccount());
        model.custom_dealer(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<SelKhBean>() {
                    @Override
                    public void onNext(SelKhBean selKhBean) {
                        if (selKhBean.issucc){
                            if (selKhBean.getData().get(0).getAppoints().size()>0){
                                for (SelKhBean.DataDTO.AppointsDTO data:selKhBean.getData().get(0).getAppoints()) {
                                    SelKhItemVM itemVM = new SelKhItemVM(SelKhVM.this,data);
                                    observableListJxs.add(itemVM);
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void setBack(SelKhBean.DataDTO.AppointsDTO appointsDTO) {
        Intent intent = new Intent();
        intent.putExtra("name",appointsDTO.getName());
        intent.putExtra("id",appointsDTO.getId()+"");
        mActivity.setResult(2,intent);
        mActivity.finish();

    }
}
